const defaultState = {
	inputValue: '',
	list: []
}

const reducer = (state = defaultState, action) => {
	const newState = JSON.parse(JSON.stringify(state));
	if (action.type === 'input_change') {
		newState.inputValue = action.value;
		return newState;
	}
	if (action.type === 'add_item') {
		if (newState.inputValue !== '') {
			newState.list.push(newState.inputValue);
			newState.inputValue = '';
			return newState;
		}
	}

	if (action.type === 'delete_item') {
		newState.list.splice(action.index, 1);
		return newState;
	}

	if (action.type === 'init_list') {
		newState.list = [...action.data];
		return newState;
	}
	return state;
}

export default reducer;